package com.jt.service.impl;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class FileServiceImpl implements FileService {
    //定义根路径
    //private String rootDir = "R:/project3/images";
    //定义Linux路径
    private String rootDir = "/usr/local/src/images";
    //定义根网址
    private String rootUrl = "http://image.jt.com";

    /**
     * 思路：
     * 1.校验文件是否为图片     校验图片类型
     * 2.防止文件为恶意程序     木马.exe .jpg
     * 3.分目录存储     按照时间维度划分
     * 4.防止文件重名    UUID
     */
    @Override
    public ImageVO fileUpload(MultipartFile file) {
        //1.校验文件是否为图片,获取图片名称     校验图片类型
        //a.jpg  jpg/png/gif
        //全转小写，以免不能传入大写
        String fileName = file.getOriginalFilename().toLowerCase();
        if (!fileName.matches("^.+\\.(jpg|png|gif)$")) {
            return null;
        }
        //2.通过校验宽度和高度判断是否为图片,bufferedImage为图片的包装对象
        try {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if (width == 0 || height == 0) {
                return null;
            }
            //3.分目录存储
            String datePath = new SimpleDateFormat("/yyyy/MM-dd/").format(new Date());
            //R:/project3/images/2021/12/16
            String fileDir = rootDir + datePath;
            File dirFile = new File(fileDir);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
            //4.动态生成UUID
            String UUID = java.util.UUID.randomUUID().toString();
            int index = fileName.lastIndexOf(".");
            String fileType = fileName.substring(index);
            fileName = UUID + fileType;
            //5.实现文件上传
            String path = fileDir + fileName;
            file.transferTo(new File(path));
            //6.准备ImageVO对象返回
            String virtualPath = datePath + fileName; //动态路径为新的路径信息
            //String urlPath = "https://img12.360buyimg.com/pop/s590x470_jfs/t1/205354/16/17988/82004/61b1dc96Ee600f3f3/9ffa616349105df0.png.webp";
            String urlPath = rootUrl + virtualPath;
            System.out.println(urlPath);
            ImageVO imageVO = new ImageVO(virtualPath, urlPath, fileName);
            return imageVO;
        } catch (Exception e) {
            e.printStackTrace();
            //抛出运行时异常
            //一般条件下为了不影响代码结构，将检查异常转化为运行时异常
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除图片
     * 需要文件全路径
     * */
    @Override
    public void deleteFile(String virtualPath) {
        String localPath = rootDir + virtualPath;
        File file = new File(localPath);
        if (file.exists()){//如果文件存在删除
            file.delete();
        }
    }
}
